package com.ymttest.database.mongo.jproduct;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;
import com.ymt.database.MongoDBDriver;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class ProductsWapper {
	private MongoDBDriver mdd = YmtMongoDriverFactroy.getInstance_YmtProductDBdriverNewJava("Products");
	private MongoDBDriver mdd1 = YmtMongoDriverFactroy.getInstance_YmtProductDBdriverNewJava("ActivityProducts");
	
	public Map<String,Object> getByProductId(String productId) throws Exception {
		mdd.selectCollection("Products");
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"spid\":\"" + productId + "\"}",1);
	    return list.size()>0?list.get(0):null;
	}

	public List<String> getByPid(String productId) throws Exception {
		mdd.selectCollection("Catalogs");
		List<String>  list = mdd.selectPart("{\"spid\":\"" + productId + "\"}");
	    
	    return list;
	}
	
	public List<Map> getActivityProductByProductId(String productId) throws Exception {
		mdd1.selectCollection("ActivityProducts");
		ArrayList<Map> list = mdd1.selectPartLimitToMap("{\"spid\":\"" + productId + "\"}",1);
		return list;
	}

	public boolean updateActivityProductByProductId(Map<String, Object> searchMap,Map<String, Object> updateMap) throws Exception {
		mdd1.selectCollection("ActivityProducts");
		BasicDBObject queryObject = new BasicDBObject(searchMap);
		BasicDBObject updateObject = new BasicDBObject("$set", updateMap);
		WriteResult wr = mdd1.getCollection().update(queryObject, updateObject,false,true);
		return  wr.isUpdateOfExisting();
	}

	public boolean insertActivityProductByProductId(Map map) throws Exception {
		mdd1.selectCollection("ActivityProducts");
		BasicDBObject queryObject = new BasicDBObject(map);
		WriteResult wr = mdd1.getCollection().save(queryObject);
		return  wr.isUpdateOfExisting();
	}
	     
	public int getCountByProductId(String productId) throws Exception{
		mdd.selectCollection("Products");
		return mdd.selectPartLimitToMap("{\"spid\":\"" + productId + "\"}",50).size();
	}
	/**
	 * 根据直播Id获取商品
	 * @param spid
	 * @return
	 * @throws Exception
	 */
	public ArrayList<String> getProductsBySpid(String spid) throws Exception {
		mdd.selectCollection("Products");
		ArrayList<String> list = mdd.selectPart("{\"spid\":\"" + spid + "\"}");
		return list;
	}   
	/**
	 * 根据直播Id获取直播商品
	 * @param lid
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getProductsBySpid(String spid,int productNum) throws Exception {
		mdd.selectCollection("Products");
		Date date=new Date();
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"spid\":\"" + spid + "\"}","{\"ispsp\":1,\"spid\":1,\"pics\":1,\"minp\":1}",productNum,"{sort:1}");
		return list;
	}
	
	public boolean updateAll(Map<String, Object> searchMap,Map<String, Object> updateMap) throws Exception{
		mdd.selectCollection("Products");
		BasicDBObject queryObject = new BasicDBObject(searchMap);
		BasicDBObject updateObject = new BasicDBObject("$set", updateMap);
		WriteResult wr = mdd.getCollection().update(queryObject, updateObject,false,true);
		return  wr.isUpdateOfExisting();
	}
	/**
	 * //条件表达式：$ge(>)  $get(>=)  $lt(<)  $lte(<=)  $ne(<>)  $in  $nin  $all $exists $or  $nor $where $type等等  
	 * @param search 搜索条件
	 *  @param orderBy sort 排序
	 * @return
	 * @throws Exception
	 */
	public List<Map> selectProduct(BasicDBObject search,DBObject orderBy) throws Exception{
		mdd.selectCollection("Products");
		List<Map> products=new ArrayList<>();
		BasicDBObject queryObject = new BasicDBObject(search);
		DBCursor db = mdd.getCollection().find(queryObject);
		if(orderBy!=null){
			db.sort(orderBy);
		}
		db.forEach(action->{
			products.add(action.toMap());
		});
		
		return products;
	}
	/**
	 * //条件表达式：$ge(>)  $get(>=)  $lt(<)  $lte(<=)  $ne(<>)  $in  $nin  $all $exists $or  $nor $where $type等等  
	 * @param search 搜索条件
	 *  @param orderBy sort 排序
	 * @return
	 * @throws Exception
	 */
	public List<Map> selectCatalogs(BasicDBObject search,DBObject orderBy) throws Exception{
		mdd.selectCollection("Catalogs");
		List<Map> Catalogs=new ArrayList<>();
		BasicDBObject queryObject = new BasicDBObject(search);
		DBCursor db = mdd.getCollection().find(queryObject);
		if(orderBy!=null){
			db.sort(orderBy);
		}
		db.forEach(action->{
			Catalogs.add(action.toMap());
		});
		
		return Catalogs;
	}
	/**
	 * //条件表达式：$ge(>)  $get(>=)  $lt(<)  $lte(<=)  $ne(<>)  $in  $nin  $all $exists $or  $nor $where $type等等  
	 * @param search 搜索条件
	 *  @param orderBy sort 排序
	 * @return
	 * @throws Exception
	 */
	public List<Map> selectActivityProducts(BasicDBObject search,DBObject orderBy) throws Exception{
		mdd.selectCollection("ActivityProducts");
		List<Map> ActivityProducts=new ArrayList<>();
		BasicDBObject queryObject = new BasicDBObject(search);
		DBCursor db = mdd.getCollection().find(queryObject);
		if(orderBy!=null){
			db.sort(orderBy);
		}
		db.forEach(action->{
			ActivityProducts.add(action.toMap());
		});
		
		return ActivityProducts;
	}
	public Map<String,Object> getProductDescExtraByProductId(String productId) throws Exception {
		mdd.selectCollection("ProductDescExtra");
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"spid\":\"" + productId + "\"}",1);
	    return list.size()>0?list.get(0):null;
	}
	public static void main(String[] args) throws Exception {
		//		System.out.println(new ProductsWapper().updateAll(MapUtil.hashMap("sid", 20336378, "isnew", true),MapUtil.hashMap("isnew", false)));
		//Mongo
		BasicDBObject search=new BasicDBObject();
		search.append("sid", 288484);
		search.append("isnew",true);
		search.append("newstart",new BasicDBObject("$lte",new Date()));  
		search.append("newend",new BasicDBObject("$gte",YMTDateUtil.getBeforeOrNextDate(new Date(), -7)));  
		System.out.println(search.toMap());
		BasicDBObject orderBy=new BasicDBObject("newstart",-1);
		System.out.println(new ProductsWapper().selectProduct(search, orderBy));
	}
	    
}
